import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
    }
    //这个是二叉树的根结点
    //public TreeNode root;

    public TreeNode createTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    //前序遍历
    void preOrder(TreeNode root){
        if(root == null) return;;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
//    public List<Integer> preorderTraversal(TreeNode root){
//        List<Integer> ret = new ArrayList<>();
//        if(root == null) return ret;
//        ret.add(root.val);
//        List<Integer> leftTree = preorderTraversal(root.left);
//        ret.addAll(leftTree);
//        List<Integer> rightTree = preorderTraversal(root.right);
//        ret.addAll(rightTree);
//        return ret;
//    }
    //中序遍历
    void inOrder(TreeNode root){
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    //后序遍历
    void postOrder(TreeNode root){
        if (root == null) return;
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    //子问题思路  获取树中结点的个数
    int size(TreeNode root){
        if (root == null) return 0;
        return size(root.left) + size(root.right) + 1;
    }
    //遍历思路  只要遍历到了结点  就nodeSize++
    public static int nodeSize;
    void size2(TreeNode root){
        if (root == null) return;
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }
    //获取叶子结点的个数
    int getLeafNodeCount(TreeNode root){
        if (root == null) return 0;
        if (root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
    //遍历问题
    public static int leafSize;
    void getLeafNodeCount2(TreeNode root){
        if (root == null) return;
        if (root.left == null && root.right == null){
            leafSize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
    }
    //获取第k层的结点个数
    int getKLevelNodeCount(TreeNode root, int k){
        if (root == null) return 0;
        if (k == 1){
            return 1;
        }

        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }
    //获取二叉树的高度
    int getHeight(TreeNode root){
        if (root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return (leftHeight > rightHeight ? leftHeight+1 : rightHeight+1);
    }
    //检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val){
        if (root == null) return null;
        if (root.val == val) return root;
        TreeNode ret1 = find(root.left,val);
        if (ret1 != null){
            return ret1;
        }
        TreeNode ret2 = find(root.right,val);
        if (ret2 != null){
            return ret2;
        }

        return null;
    }
    //层序遍历

    void levelOrder(TreeNode root){
        if (root == null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        //先将根节点放进去
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            //只要子树的左右节点不为null就给队列里面放
            if (cur.left != null){
                queue.offer(cur.left);
            }
            if (cur.right != null){
                queue.offer(cur.right);
            }
        }
        System.out.println();

    }
    //判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        if (root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else{
                break;
            }
        }
        while(!queue.isEmpty()){
            TreeNode cur = queue.peek();
            if (cur != null){
                return false;
            }else{
                queue.poll();
            }
        }
        return true;
    }

}
